package com.example.mydy.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.mydy.R;
import com.example.mydy.adapter.RewardPagerAdapter;
import com.example.mydy.utils.MockDataManager;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

/**
 * 悬赏任务界面的主Fragment，包含可接取悬赏和我的悬赏两个子标签页
 */
public class RewardFragment extends Fragment {
    
    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private RewardPagerAdapter pagerAdapter;
    private MockDataManager mockDataManager;

    public RewardFragment() {
        // 必需的空构造函数
    }

    public static RewardFragment newInstance() {
        return new RewardFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_reward, container, false);
        
        mockDataManager = MockDataManager.getInstance();
        
        // 初始化视图组件
        initViews(view);
        
        return view;
    }

    private void initViews(View view) {
        // 初始化ViewPager2和TabLayout
        viewPager = view.findViewById(R.id.reward_view_pager);
        tabLayout = view.findViewById(R.id.reward_tab_layout);
        
        // 设置ViewPager适配器
        FragmentManager fragmentManager = getChildFragmentManager();
        pagerAdapter = new RewardPagerAdapter(fragmentManager, getLifecycle());
        viewPager.setAdapter(pagerAdapter);
        
        // 关联TabLayout和ViewPager2
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("可接取悬赏");
                    break;
                case 1:
                    tab.setText("我的悬赏");
                    break;
            }
        }).attach();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 当Fragment恢复时，刷新数据
        if (pagerAdapter != null) {
            pagerAdapter.refreshData();
        }
    }
} 